package tech.aichan.AiQA.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import tech.aichan.AiQA.common.BaseResponse;
import tech.aichan.AiQA.common.DeleteRequest;
import tech.aichan.AiQA.model.dto.question.*;
import tech.aichan.AiQA.model.entity.Question;
import tech.aichan.AiQA.model.vo.question.QuestionVO;
import tech.aichan.AiQA.service.QuestionService;
import tech.aichan.AiQA.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
* 题目接口
*
* @author mengyu
* @date 2024/06/03 10:13
*/
@RestController
@RequestMapping("/question")
@Slf4j
public class QuestionController {

    @Resource
    private QuestionService questionService;

    @Resource
    private UserService userService;

    // region 增删改查

    /**
     * 创建题目
     *
     * @param questionAddRequest
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addQuestion(@RequestBody @Valid QuestionAddRequest questionAddRequest) {
        Long qId = questionService.addQuestion(questionAddRequest);
        return BaseResponse.success(200,qId);
    }

    /**
     * 删除题目
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteQuestion(@RequestBody @Valid DeleteRequest deleteRequest) {
        Long id = deleteRequest.getId();
        questionService.deleteQuestion(id);
        return BaseResponse.success(200,true);
    }

    /**
     * 更新题目（仅管理员可用）
     *
     * @param questionUpdateRequest
     * @return
     */
    @PreAuthorize("hasAnyRole('admin')")
    @PostMapping("/update")
    public BaseResponse<Boolean> updateQuestion(@RequestBody QuestionUpdateRequest questionUpdateRequest) {
        Long id = questionUpdateRequest.getId();
        List<QuestionContentDTO> questionContent = questionUpdateRequest.getQuestionContent();
        questionService.updateQuestion(questionContent,id);
        return BaseResponse.success(200,true);
    }

    /**
     * 根据 id 获取题目（封装类）
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<QuestionVO> getQuestionVOById(long id) {
        Question question = questionService.getById(id);
        return BaseResponse.success(200,QuestionVO.getQuestionVOByQuestion(question,userService.getById(question.getUserId())));
    }

    /**
     * 分页获取题目列表（仅管理员可用）
     *
     * @param questionQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    @PreAuthorize("hasAnyRole('admin')")
    public BaseResponse<Page<Question>> listQuestionByPage(@RequestBody @Valid QuestionQueryRequest questionQueryRequest) {
        return BaseResponse.success(200,questionService.listPageQuestion(questionQueryRequest));
    }

    /**
     * 分页获取题目列表（封装类）
     *
     * @param questionQueryRequest
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<QuestionVO>> listQuestionVOByPage(@RequestBody QuestionQueryRequest questionQueryRequest) {
        return BaseResponse.success(200,questionService.listPageQuestionVO(questionQueryRequest));
    }

    /**
     * 编辑题目（给用户使用）
     *
     * @param questionEditRequest
     * @return
     */
    @PostMapping("/edit")
    public BaseResponse<Boolean> editQuestion(@RequestBody QuestionEditRequest questionEditRequest) {
        List<QuestionContentDTO> questionContent = questionEditRequest.getQuestionContent();
        Long id = questionEditRequest.getId();
        questionService.editQuestion(questionContent,id);
        return BaseResponse.success(200,true);
    }

    // endregion
}
